package com.tfy.liquor.service.impl;

import com.tfy.liquor.bean.ProductOrder;
import com.tfy.liquor.bean.UserShoppingAddress;
import com.tfy.liquor.dao.ProductOrderDao;
import com.tfy.liquor.dao.UserShoppingAddressDao;
import com.tfy.liquor.service.ProductOrderService;
import com.tfy.liquor.service.UserPurseService;
import com.tfy.liquor.util.CodeUtils;
import com.tfy.liquor.util.DateUtil;
import com.tfy.liquor.util.SnowFlake;
import com.tfy.liquor.util.poi.ExportExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class ProductOrderServiceImpl implements ProductOrderService {
    @Autowired
    private ProductOrderDao productOrderDao;
    @Autowired
    private UserShoppingAddressDao userShoppingAddressDao;
    @Autowired
    private UserPurseService userPurseService;

    // 获取一个订单编号
    @Override
    public String getOrderId() {
        return CodeUtils.getOrderId();
    }

    // 创建订单信息
    @Override
    public Boolean createProductOrder(ProductOrder productOrder) {
        // 生成一个编号
        productOrder.setId(SnowFlake.getId());
        // 生成订单编号
        productOrder.setOrderId(CodeUtils.getOrderId());
        // 判断备注信息
        if (null == productOrder.getRemark() || "".equals(productOrder.getRemark().trim())) {
            // 如果没有备注信息
            productOrder.setRemark("空");
        }
        // 设置订单创建时间
        productOrder.setCreateTime(DateUtil.getNowDateTime());
        return productOrderDao.createProductOrder(productOrder) > 0;
    }

    // 导出订单信息
    @Override
    public void exportProductOrder(HttpServletResponse response, ProductOrder productOrder) {
        ArrayList<Map<String, String>> resultList = new ArrayList<>();
        // 获取订单列表
        ArrayList<ProductOrder> orderList = productOrderDao.getProductOrder(productOrder);
        // 根据订单查询收货地址
        for (ProductOrder order : orderList) {
            HashMap<String, String> map = new HashMap();
            // 获取收货地址信息
            UserShoppingAddress userShoppingAddress = userShoppingAddressDao.getUserShoppingById(order.getAddressId());
            // 订单基本信息
            map.put("编号", order.getId());
            map.put("下单用户编号", order.getUserId() + "");
            map.put("父订单编号", order.getParentOrderId());
            map.put("订单编号", order.getOrderId());
            map.put("产品编号", order.getProductId());
            map.put("产品编码", order.getProductEncode() + "");
            map.put("产品名称", order.getProductName());
            map.put("进货价", order.getPurchasePrice() + "");
            map.put("实际支付金额", order.getProductEncode() + "");
            map.put("数量", order.getNumber() + "");
            map.put("付款方式，1》现金，2》微信，3》支付宝，4》银行卡，5》优惠券，6》其他", order.getPaymentMethod() + "");
            map.put("订单状态，0》已完成，1》待支付，2》待发货，3》已取消，4》待收货，5》其他", order.getStatus() + "");
            // 收货地址信息
            map.put("收货人姓名", userShoppingAddress.getUserName());
            map.put("收货手机号码", userShoppingAddress.getMobile());
            map.put("收货地址", userShoppingAddress.getProvinceName() + userShoppingAddress.getCityName() + userShoppingAddress.getAreaName() + userShoppingAddress.getTownName() + userShoppingAddress.getDetail());
            // 订单其他信息
            map.put("订单备注", order.getRemark() + "");
            map.put("创建时间", order.getCreateTime() + "");
            map.put("付款时间", order.getPayTime() + "");
            map.put("收货时间", order.getReceiptTime() + "");
            map.put("完成时间", order.getFinishTime() + "");

            // 将数据添加到resultLIst中
            resultList.add(map);
        }
        // 导出数据
        ExportExcelUtil.exportExcel(response, resultList, "orderInfo");
    }

    // 根据父订单编号获取付款金额
    @Override
    public String getOrderAmountByParentOrderId(Long userId, String parentOrderId) {
        return productOrderDao.getOrderAmountByParentOrderId(userId, parentOrderId);
    }

    // 订单付款
    @Override
    public Boolean payProductOrder(ProductOrder productOrder) {
        if (productOrderDao.payProductOrder(productOrder) > 0) { // 如果付款成功
            userPurseService.orderEarnings(productOrder);
        }
        return false;
    }


}
